package com.my.study.common.config;

import com.my.study.common.properties.RedissonProperties;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Redisson配置类
 *
 * <p>https://developer.aliyun.com/article/551640#slide-19</p>
 * 
 * @author: yidujun
 * @create: 2021/03/15 15:15
 */
@Configuration
@EnableConfigurationProperties(RedissonProperties.class)
public class RedissonConfig {

    private static final String REDIS_PREFIX = "redis://";

    /**
     * 单节点模式
     *
     * @return RedissonClient
     */
    @Bean(name = "redissonClient")
    @ConditionalOnProperty(prefix = "my.redis", name = "model", havingValue = "single")
    public RedissonClient redissonClient(RedissonProperties properties) {
        Config config = new Config();
        config.useSingleServer()
                .setAddress(REDIS_PREFIX + properties.getIp())
                .setPassword(null);
        return Redisson.create(config);
    }

    /**
     * 哨兵模式
     *
     * @return
     */
//    @Bean(destroyMethod = "shutdown")
//    @ConditionalOnProperty(name = "redis.model", havingValue = "sentinel")
//    public RedissonClient redissonClientOfSentinel() {
//        Config config = new Config();
//        SentinelServersConfig sentinelServersConfig = config.useSentinelServers().setMasterName("");
//        String[] ips = "".split(",");
//        sentinelServersConfig.addSentinelAddress(ips);
//        return Redisson.create(config);
//    }

    /**
     * 主从模式
     *
     * @return
     */
//    @Bean(destroyMethod = "shutdown")
//    @ConditionalOnProperty(name = "redis.model", havingValue = "masterSlave")
//    public RedissonClient redissonClientOfMasterSlave() {
//        Config config = new Config();
//        MasterSlaveServersConfig masterSlaveServersConfig = config.useMasterSlaveServers().setPassword("").setMasterAddress(REDIS_PREFIX + "127.0.0.1:6379");
//        masterSlaveServersConfig.addSlaveAddress("".split(","));
//        return Redisson.create(config);
//    }


    /**
     * 集群模式
     *
     * @return RedissonClient
     */
//    @Bean(destroyMethod = "shutdown")
//    @ConditionalOnProperty(name = "redis.model", havingValue = "single")
//    public RedissonClient redissonClientCluster() {
//        Config config = new Config();
//        ClusterServersConfig clusterServersConfig = config.useClusterServers().setPassword("");
//        String[] ips = "".split(",");
//        for (String ip : ips) {
//            clusterServersConfig.addNodeAddress(REDIS_PREFIX + ip);
//        }
////        config.useClusterServers()
////                // 集群状态扫描间隔时间，单位是毫秒
////                .setScanInterval(2000)
////                .addNodeAddress("")
////                .addNodeAddress("");
//
//        return Redisson.create(config);
//    }
}
